<template>
  <div class="xtx-goods-page">
    <div class="container"
         v-if="goodsDetail">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsDetail.categories[1].id}`">{{
          goodsDetail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsDetail.categories[1].id}`">{{
          goodsDetail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodsDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :imageList="goodsDetail.mainPictures" />
          <GoodsSale :goodsDetail="goodsDetail" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品信息组件 -->
          <GoodsInfo :goodsDetail="goodsDetail" />
          <!-- 商品规格组件 -->
          <XtxGoodSku :goods="goodsDetail"
                      @change="goodsSkuChangeFn" />
          <!-- 商品数量组件 -->
          <XtxNumbox v-model="count"
                     :min="count"
                     :max="goodsDetail.inventory" />
          <!-- 按钮组件 -->
          <XtxButton @click="addToCart"
                     type="primary"
                     style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetails :goods="goodsDetail" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <GoodsHot :goodsId='goodsDetail.id' :type="1"/>
            <GoodsHot :goodsId='goodsDetail.id' :type="2"/>
            <GoodsHot :goodsId='goodsDetail.id' :type="3"/>
        </div>
      </div>
    </div>
    <div class="container"
         v-else>
      <h1>loading...</h1>
    </div>
  </div>
</template>

<script>
import GoodsInfo from './components/goods-info.vue'
import GoodsSale from './components/goods-sale.vue'
import { findGoodsDetail } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import GoodsDetails from './components/goods-details.vue'
import GoodsHot from './components/goods-hot.vue'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsInfo, GoodsSale, GoodsDetails, GoodsHot },
  setup () {
    const route = useRoute()
    const goodsDetail = ref(null)
    const saveGoodsDetail = ref(null)
    // 商品数量
    const count = ref(1)
    findGoodsDetail(route.params.id).then(({ result }) => {
      goodsDetail.value = result
      saveGoodsDetail.value = result
    })
    // 商品规格选择
    const goodsSkuChangeFn = (val) => {
      //   console.log('val------->', val)
      // 判断用户是否选择了商品规格，若选择了，则更改渲染页面商品数据，若没有选择或将选择清空，返回原来的数据
      if (val.skuId) {
        for (const key in val) {
          goodsDetail.value[key] = val[key]
        }
      } else {
        goodsDetail.value = { ...saveGoodsDetail.value }
      }
    }
    const addToCart = () => {
      console.log(12345)
    }
    return { count, goodsDetail, goodsSkuChangeFn, addToCart }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
